package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Theme;
import com.ws.lv.service.ThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/theme")
public class ThemeController {
    @Autowired
    ThemeService themeService;

    //主题列表
    @RequestMapping("/selectTheme")
    public String selectTheme() {
        return "adminpage/theme/list";
    }

    //去添加主题页面
    @RequestMapping("/toAddTheme")
    public String toAddTheme() {
        return "adminpage/theme/add";
    }

    //去修改主题页面
    @RequestMapping("/toUpdateTheme")
    public String toUpdateTheme(int theme_id, Model model) {
        System.out.println("去修改主题页面");
        Theme theme = themeService.selectThemeById(theme_id);
        model.addAttribute("theme", theme);
        return "adminpage/theme/update";
    }

    //多条件主题查询
    @RequestMapping("/selectThemeByTerm")
    public String selectThemeByTerm(String theme_name, Integer theme_state, String theme_order, Model model,Integer currentPage) {
        System.out.println("多条件查询主题");

        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null){
            pageHelper.startPage(1, 5);
        }else {
            pageHelper.startPage(currentPage, 5);
        }
        List<Theme> themes = themeService.selectThemeByTerm(theme_name,theme_state,theme_order);
        PageInfo pageInfo = new PageInfo(themes);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("theme_name", theme_name);
        model.addAttribute("theme_state", theme_state);
        model.addAttribute("theme_order", theme_order);
        return "adminpage/theme/list";
    }

    //添加主题
    @RequestMapping("/insertTheme")
    public String insertTheme(Theme theme, Model model) {
        int i = themeService.insertTheme(theme);
        return "redirect:/theme/selectThemeByTerm";
    }

    //修改主题显示状态
    @RequestMapping("/updateThemeDisplay")
    public String updateThemeDisplay(int theme_id) {
        System.out.println("修改主题显示状态");
        System.out.println(theme_id);
        int i = themeService.updateThemeDisplay(theme_id);
        return "forward:/theme/selectThemeByTerm";
    }

    //修改主题隐藏状态
    @RequestMapping("/updateThemeHide")
    public String updateThemeHide(int theme_id) {
        System.out.println("修改主题隐藏状态");
        System.out.println(theme_id);
        int i = themeService.updateThemeHide(theme_id);
        return "forward:/theme/selectThemeByTerm";
    }

    //修改主题
    @RequestMapping("/updateTheme")
    public String updateTheme(Theme theme) {
        System.out.println("修改主题");
        System.out.println(theme);
        int i = themeService.updateTheme(theme);
        return "redirect:/theme/selectThemeByTerm";
    }

    //删除多个主题
    @RequestMapping("/deleteThemes")
    public String deleteThemes(String theme_ids) {
        System.out.println("删除多个主题");
        if (theme_ids != null && theme_ids.trim().length() > 0){
            String[] split = theme_ids.split(",");
            ArrayList<Integer> integers = new ArrayList<>();
            for (String s : split) {
                integers.add(Integer.parseInt(s));
            }
            int i = themeService.deleteThemes(integers);
        }
        return "redirect:/theme/selectThemeByTerm";
    }



}
